<html>
<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@page import="weaver.general.Util" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%
    SimpleDateFormat format = new SimpleDateFormat("HH-mm");
    String date = format.format(new Date());
    int nodeid = Util.getIntValue(request.getParameter("nodeid"));//流程的节点id
    RecordSet rst = new RecordSet();
    String isstart = "0";
    rst.executeQuery("select isstart from WORKFLOW_NODEBASE where id=" + nodeid);//工作流节点信息表中的当前节点是否为创建节点字段
    if (rst.next()) {
        isstart = Util.null2String(rst.getString("isstart"));
    }
%>

<head>
</head>
<body>
</body>
<script type="text/javascript">
    var zsdata = "0";
    var index = 0;
    var isstart = '<%=isstart%>';//1为创建节点
    var nodeid = '<%=nodeid%>';
    var datetime = '<%=date%>';
    var zs_fieldid_dt1 = WfForm.convertFieldNameToId("zs", "detail_1");//桌数
    var lkrs_fieldid_dt1 = WfForm.convertFieldNameToId("lkrs", "detail_1");//来客人数
    var ptrs_fieldid_dt1 = WfForm.convertFieldNameToId("ptrs", "detail_1");//陪同人数
    var jcrq_fieldid_dt1 = WfForm.convertFieldNameToId("jcrq", "detail_1");//聚餐日期
    var yclx_fieldid_dt1 = WfForm.convertFieldNameToId("yclx", "detail_1");//用餐类型
    var zrs_fieldid_dt1 = WfForm.convertFieldNameToId("zrs", "detail_1");//总人数
    jQuery(document).ready(function () {
        //提交保存
        WfForm.registerCheckEvent(WfForm.OPER_SAVE + "," + WfForm.OPER_SUBMIT, function (callback) {

            if (checkSubmitZrs()) {
                callback();
            }

        });

        if (isstart == "1") {

            WfForm.bindDetailFieldChangeEvent(ptrs_fieldid_dt1, function (id, rowIndex, value) {
                if(value !='' && value*1.0>3){
                    alert("陪同人数不能大于3人！");
                    WfForm.changeFieldValue(ptrs_fieldid_dt1 + "_" + rowIndex, {value: ""});
                }
            });

            WfForm.bindDetailFieldChangeEvent(zrs_fieldid_dt1, function (id, rowIndex, value) {
                rs(rowIndex,value);
            });
            WfForm.bindDetailFieldChangeEvent(zs_fieldid_dt1, function (id, rowIndex, value) {
                zs1(rowIndex);
            });

            WfForm.bindDetailFieldChangeEvent(jcrq_fieldid_dt1, function (id, rowIndex, value) {
                if (value != '' && value < getNowFormatDate()) {
                    alert("日期不能小于当天。");
                    WfForm.changeFieldValue(jcrq_fieldid_dt1 + "_" + rowIndex, {value: ""});
                }
                WfForm.changeFieldValue(yclx_fieldid_dt1 + "_" + rowIndex, {value: ""});
                WfForm.changeFieldValue(zrs_fieldid_dt1 + "_" + rowIndex, {value: ""});
                WfForm.changeFieldValue(zs_fieldid_dt1 + "_" + rowIndex, {value: ""});
            });

            WfForm.bindDetailFieldChangeEvent(yclx_fieldid_dt1, function (id, rowIndex, value) {

                var lx = value;
                var rq = WfForm.getFieldValue(jcrq_fieldid_dt1 + "_" + rowIndex);
                var rq1 = getNowFormatDate();
                var sj = datetime;
                var rq11 = sj.substr(0, 2) * 1.0;
                var rq22 = sj.substr(3, 5) * 1.0;//当前时间的分
                /**
                 * 3、明细行用餐类型（yclx）为午餐（0）时，时间字段（jcsj）必须不大于“9:00”，若大大于则提示：”必须提前三小时申请！“；
                 用餐类型（yclx）为晚餐（1）时，时间字段（jcsj）必须小于“14:30”；
                 */
                if (lx == "0" && rq == rq1) {
                    if (rq11 > 9 || rq11 == 9 && rq22 > 0) {
                        alert("必须提前三小时申请！");
                        WfForm.changeFieldValue(yclx_fieldid_dt1 + "_" + rowIndex, {value: ""});
                    }
                } else if (lx == "1" && rq == rq1) {
                    if ((rq11 > 14 && rq22 > 30) || rq11 >= 15) {
                        alert("必须提前三小时申请！");
                        WfForm.changeFieldValue(yclx_fieldid_dt1 + "_" + rowIndex, {value: ""});
                    }
                }
            });

        } else {
            var indexnum = WfForm.getDetailRowCount("detail_1");
            if (indexnum >= 0) {
                bindfee2();
            }
        }

    })

    //遍历明细表字段
    function bindfee2() {
        var dt1 = WfForm.getDetailAllRowIndexStr("detail_1");
        for (var i = 0; i < dt1.length; i++) {
            var indexnum1 = i;
            index = indexnum1;
            WfForm.bindDetailFieldChangeEvent(zrs_fieldid_dt1, function (id, rowIndex, value) {
                rs1(rowIndex,value)
            });

            WfForm.bindDetailFieldChangeEvent(zs_fieldid_dt1, function (id, rowIndex, value) {
                zs2(rowIndex);
            });
            WfForm.bindDetailFieldChangeEvent(ptrs_fieldid_dt1, function (id, rowIndex, value) {
                if(value !='' && value*1.0>3){
                    alert("陪同人数不能大于3人！");
                    WfForm.changeFieldValue(ptrs_fieldid_dt1 + "_" + rowIndex, {value: ""});
                }
            });

        }
    }

    //2、明细中桌数为1时控制总人数（zrs）不能大于13，若大于则提示：“每桌最多容纳13人就餐！”；
    //桌数为2时控制总人数（zrs）不能大于20，若大于则提示：“每桌最多容纳13人就餐！”；
    //桌数为3时控制总人数（zrs）不能大于30，若大于则提示：“每桌最多容纳13人就餐！”；
    function rs(indexnum1,value) {
        var zrs = value * 1.0;
        var zs = "";//当前添加的桌数，与之前添加的一起做对比
        if (zrs > 0 && zrs <= 13) {
            fz("1", indexnum1);
            zs = 1;
        } else if (zrs > 13 && zrs <= 26) {
            fz("2", indexnum1);
            zs = 2;
        } else if (zrs > 26 && zrs <= 39) {
            fz("3", indexnum1);
            zs = 3;
        } else if (zrs == 0) {
            fz("0", indexnum1);
        } else {
            zs = 4
        }
        console.log("zrs" + zrs);

        console.log("zs" + zs);
        if (zs > 0) {
            if (zrs > (zs * 13)) {
                //console.log(zrs);
                alert("每桌最多容纳13人就餐！");
                WfForm.changeFieldValue(ptrs_fieldid_dt1 + "_" + indexnum1, {value: ""});
                WfForm.changeFieldValue(lkrs_fieldid_dt1 + "_" + indexnum1, {value: ""});
                if (zrs <= 13) {
                    WfForm.changeFieldValue(zs_fieldid_dt1 + "_" + indexnum1, {value: "1"});
                } else if (zrs > 13 && zrs <= 26) {
                    WfForm.changeFieldValue(zs_fieldid_dt1 + "_" + indexnum1, {value: "2"});
                } else if (zrs > 26 && zrs <= 39) {
                    WfForm.changeFieldValue(zs_fieldid_dt1 + "_" + indexnum1, {value: "3"});
                }
            }
        }
        zs1(indexnum1);
    }

    function rs1(indexnum1,value) {
        var zrs = value * 1.0;
        var zs = "";//当前添加的桌数，与之前添加的一起做对比
        if (zrs > 0 && zrs <= 13) {
            fz("1", indexnum1);
            zs = 1;
        } else if (zrs > 13 && zrs <= 26) {
            fz("2", indexnum1);
            zs = 2;
        } else if (zrs > 26 && zrs <= 39) {
            fz("3", indexnum1);
            zs = 3;
        } else if (zrs == 0) {
            fz("0", indexnum1);
        } else {
            zs = 4
        }

        //	console.log(zs+"::"+zrs);
        if (zs > 0) {
            if (zrs > (zs * 13)) {
                //console.log(zrs);
                alert("每桌最多容纳13人就餐！");
                if (zd == "1") {
                    WfForm.changeFieldValue(ptrs_fieldid_dt1 + "_" + indexnum1, {value: ""});
                } else if (zd == "2") {
                    WfForm.changeFieldValue(lkrs_fieldid_dt1 + "_" + indexnum1, {value: ""});
                }
                if (zrs <= 13) {
                    WfForm.changeFieldValue(zs_fieldid_dt1 + "_" + indexnum1, {value: 1});
                } else if (zrs > 13 && zrs <= 26) {
                    WfForm.changeFieldValue(zs_fieldid_dt1 + "_" + indexnum1, {value: 2});
                } else if (zrs > 26 && zrs <= 39) {
                    WfForm.changeFieldValue(zs_fieldid_dt1 + "_" + indexnum1, {value: 3});
                }
            }
        }
        zs2(indexnum1);
    }

    function zs2(indexnum1) {
        var zzs = "0" * 1.0;//总桌数，用于判断、统计明细每天（jcrq）同一用餐类型（yclx）的所有明细行桌数字段（zs）之和不能大于3，若大于则提示：“每餐每天申请不得超过3桌！”
        console.log("zzs:" + zzs);
        if (index < indexnum1) {
            index = indexnum1;
        }

        var zs = WfForm.getFieldValue(zs_fieldid_dt1 + "_" + indexnum1) * 1.0;//当前添加的桌数，与之前添加的一起做对比
        var lx = WfForm.getSelectShowName(yclx_fieldid_dt1 + "_" + indexnum1);
        var rq = WfForm.getFieldValue(jcrq_fieldid_dt1 + "_" + indexnum1);
        if (lx == "午餐") {
            lx = "0";
        } else {
            lx = "1";
        }
        var zrs = WfForm.getFieldValue(lkrs_fieldid_dt1 + "_" + indexnum1) * 1.0 + WfForm.getFieldValue(ptrs_fieldid_dt1 + "_" + indexnum1) * 1.0;
        if (rq != undefined && lx != undefined) {
            getNote(rq, lx, indexnum1);
        }
        for (var i = 0; i <= indexnum1; i++) {
            var mxlx = WfForm.getSelectShowName(yclx_fieldid_dt1 + "_" + i);
            var mxrq = WfForm.getFieldValue(jcrq_fieldid_dt1 + "_" + i);
            var zrs = WfForm.getFieldValue(lkrs_fieldid_dt1 + "_" + i) * 1.0 + WfForm.getFieldValue(ptrs_fieldid_dt1 + "_" + indexnum1) * 1.0;
            var zs = "";//当前添加的桌数，与之前添加的一起做对比
            if (zrs > 0 && zrs <= 13) {
                //	fz("1",indexnum1);
                zs = 1;
            } else if (zrs > 13 && zrs <= 26) {
                //	fz("2",indexnum1);
                zs = 2;
            } else if (zrs > 26 && zrs <= 39) {
                //	fz("3",indexnum1);
                zs = 3;
            } else if (zrs == 0) {
                zs = 0;
                //		fz("0",indexnum1);
            } else {
                zs = 4
            }
            var zs1 = zs;
            console.log("zs1:" + zs1);
            if (mxlx == "午餐") {
                mxlx = "0";
            } else {
                mxlx = "1";
            }
            if (lx == mxlx && rq == mxrq) {
                console.log("zong2:" + zzs);
                zzs += zs1;
                //alert("zs=="+zs1);
            }
        }

        for (var i = WfForm.getDetailRowCount("detail_1"); i > indexnum1; i--) {
            var mxlx = WfForm.getSelectShowName(yclx_fieldid_dt1 + "_" + i);
            var mxrq = WfForm.getFieldValue(jcrq_fieldid_dt1 + "_" + i);
            var zrs = WfForm.getFieldValue(lkrs_fieldid_dt1 + "_" + i) * 1.0 + WfForm.getFieldValue(ptrs_fieldid_dt1 + "_" + indexnum1) * 1.0;
            var zs = "";//当前添加的桌数，与之前添加的一起做对比
            if (zrs > 0 && zrs <= 13) {
                //	fz("1",indexnum1);
                zs = 1;
            } else if (zrs > 13 && zrs <= 26) {
                //	fz("2",indexnum1);
                zs = 2;
            } else if (zrs > 26 && zrs <= 39) {
                //	fz("3",indexnum1);
                zs = 3;
            } else if (zrs == 0) {
                zs = 0;
                //		fz("0",indexnum1);
            } else {
                zs = 4
            }
            ;
            var zs1 = zs;
            console.log("zs11:" + zs1);
            if (mxlx == "午餐") {
                mxlx = "0";
            } else {
                mxlx = "1";
            }
            if (lx == mxlx && rq == mxrq) {
                console.log("zong1:" + zzs);
                zzs += zs1;
                //alert("zs=="+zs1);
            }
        }
        console.log("zong:" + zzs);
        zzs += zsdata;
        console.log("总数：" + zzs);
        if (zzs > 3) {
            alert("每餐每天申请不得超过3桌！");
            fz("", indexnum1);
        }
        /*
        * 2、明细中桌数为1时控制总人数（zrs）不能大于13，若大于则提示：“每桌最多容纳13人就餐！”；
             桌数为2时控制总人数（zrs）不能大于20，若大于则提示：“每桌最多容纳13人就餐！”；
             桌数为3时控制总人数（zrs）不能大于30，若大于则提示：“每桌最多容纳13人就餐！”；
        * */
        if (zs > 0 && zrs > (zs * 13)) {
            alert("每桌最多容纳13人就餐！");
            fz("", indexnum1);
        }

    }

    //1、总桌数，用于判断、统计明细每天（jcrq）同一用餐类型（yclx）的所有明细行桌数字段（zs）之和不能大于3，
    // 若大于则提示：“每餐每天申请不得超过3桌！”
    function zs1(indexnum1) {
        var zzs = "0" * 1.0;
        var zs = WfForm.getFieldValue(zs_fieldid_dt1 + "_" + indexnum1);
        var lx = WfForm.getFieldValue(yclx_fieldid_dt1 + "_" + indexnum1);
        var rq = WfForm.getFieldValue(jcrq_fieldid_dt1 + "_" + indexnum1);
        var lkrs_fieldvalue = WfForm.getFieldValue(lkrs_fieldid_dt1 + "_" + indexnum1);
        var ptrs_fieldvalue = WfForm.getFieldValue(ptrs_fieldid_dt1 + "_" + indexnum1);
        var zrs = lkrs_fieldvalue + ptrs_fieldvalue;//总人数
        if (rq != undefined && lx != undefined) {
            getNote(rq, lx, indexnum1);
        }
        for (var i = 0; i <= indexnum1; i++) {
            var mxlx = WfForm.getFieldValue(yclx_fieldid_dt1 + "_" + i);
            var mxrq = WfForm.getFieldValue(jcrq_fieldid_dt1 + "_" + i);
            var zs1 = WfForm.getFieldValue(zs_fieldid_dt1 + "_" + i) * 1.0;
            if (lx == mxlx && rq == mxrq) {
                zzs += zs1;
            }
        }
        for (var i = index; i > indexnum1; i--) {
            var mxlx = WfForm.getFieldValue(yclx_fieldid_dt1 + "_" + i);
            var mxrq = WfForm.getFieldValue(jcrq_fieldid_dt1 + "_" + i);
            var zs1 = WfForm.getFieldValue(zs_fieldid_dt1 + "_" + i);
            if (yclx_fieldvalue == mxlx && jcrq_fieldvalue == mxrq) {
                zzs += zs1;
            }
        }
        console.log("zong:" + zzs);
        zzs += zsdata;
        console.log("总数：" + zzs);
        if (zzs > 3) {
            alert("每餐每天申请不得超过3桌！");
            fz("", indexnum1);
        }
        /*
        * 2、明细中桌数为1时控制总人数（zrs）不能大于13，若大于则提示：“每桌最多容纳13人就餐！”；
             桌数为2时控制总人数（zrs）不能大于20，若大于则提示：“每桌最多容纳13人就餐！”；
             桌数为3时控制总人数（zrs）不能大于30，若大于则提示：“每桌最多容纳13人就餐！”；
        * */
        // if (zs > 0 && zrs > (zs * 13)) {
        //     alert("每桌最多容纳13人就餐！");
        //     fz("", indexnum1);
        // }

    }

    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;

        return currentdate;
    }

    function fz(value, indexnum1) {
        WfForm.changeFieldValue(zs_fieldid_dt1 + "_" + indexnum1, {value: value});
    }

    //获取桌数Ajax方法
    function getNote(rq, lx, indexnum, isstart, requestid) {
        jQuery.ajax({
            type: "POST",
            url: "/interface/NoteAjax.jsp?rq=" + rq + "&lx=" + lx + "&isstart=" + isstart + "&requestid=" + requestid + "&indexnum=" + indexnum,
            async: false,
            success: function (res) {
                zsdata = res * 1.0;
                console.log("zsdata11++" + zsdata);
            },
            error: function (e) {
                alert("调用接口出错！wrong!");
            }
        });
    }

    //4、明细行总人数必须大于0，否则提示：“总人数为0时，无法提交，请检查”；
    function checkSubmitZrs(isstart) {
        var flag = true;
        if (isstart == "1") {
            var dt1 = WfForm.getDetailAllRowIndexStr("detail_1");
            for (var i = 0; i < dt1.length; i++) {
                var zrs_fieldvalue = WfForm.getFieldValue(zrs_fieldid_dt1 + "_" + dt1[i]);
                if (zrs_fieldvalue * 1.0 <= 0) {
                    alert("总人数为0时，无法提交，请检查");
                    flag = false;
                    continue;
                }
            }
        }
        return flag;
    }

</script>
</html>